package com.chenfan.mcn.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.enums.LogBusinessTypeEnum;
import com.chenfan.mcn.event.EventPublisher;
import com.chenfan.mcn.event.OperateLogEvent;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.StarContract;
import com.chenfan.mcn.dao.StarContractMapper;
import com.chenfan.mcn.service.StarContractService;
import com.chenfan.mcn.utils.BusinessAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 红人签约合同 服务实现类
 * </p>
 *
 * @author wulg
 * @since 2021-07-20
 */
@Service
public class StarContractServiceImpl implements StarContractService {

    @Autowired
    private StarContractMapper starContractMapper;

    @Override
    public boolean termination(Long id) {
        StarContract entity = starContractMapper.selectOne(Wrappers.lambdaQuery(StarContract.class).eq(StarContract::getId, id).eq(StarContract::getIsDelete, Boolean.FALSE));
        BusinessAssert.notNull(entity, McnErrorCode.DATA_NOT_FOUND);
        StarContract updateEntity = new StarContract();
        updateEntity.setId(id);
        updateEntity.setState(NumberCst.ONE);
        updateEntity.setRelieveTime(new Date());
        boolean result = starContractMapper.updateById(updateEntity) > 0;
        if(result) {
            //操作日志
            EventPublisher.post(OperateLogEvent.builder().businessId(entity.getStarId())
                    .businessType(LogBusinessTypeEnum.STAR_CONTRACT)
                    .compare(entity, starContractMapper.selectById(id))
                    .build());
        }
        return result;
    }
}
